<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Mybatis快速入门一(基本应用) | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="SSM">
    <meta name="description" content="Mybatis介绍什么是 Mybatismybatis就是一个封装来jdbc的持久层框架，它和hibernate都属于ORM框架，但是具体的说，hibernate是一个完全的orm框架，而mybatis是一个不完全的orm框架。mybatis让程序员只关注sql本身，而不需要去关注如连接的创建，statement的创建等操作。mybatis会将输入的参数，输出结果进行映射。 Mybatis框架原理">
<meta name="keywords" content="SSM">
<meta property="og:type" content="article">
<meta property="og:title" content="Mybatis快速入门一(基本应用)">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="Mybatis介绍什么是 Mybatismybatis就是一个封装来jdbc的持久层框架，它和hibernate都属于ORM框架，但是具体的说，hibernate是一个完全的orm框架，而mybatis是一个不完全的orm框架。mybatis让程序员只关注sql本身，而不需要去关注如连接的创建，statement的创建等操作。mybatis会将输入的参数，输出结果进行映射。 Mybatis框架原理">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/mybatis%E5%8E%9F%E7%90%86%E5%9B%BE.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/mybatis%E6%95%B0%E6%8D%AE%E5%BA%93%E6%88%AA%E5%9B%BE.png">
<meta property="og:image" content="http://om6odecqr.bkt.clouddn.com/mybatis%E9%A1%B9%E7%9B%AE%E5%87%86%E5%A4%87.png">
<meta property="og:updated_time" content="2017-12-05T09:57:52.006Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mybatis快速入门一(基本应用)">
<meta name="twitter:description" content="Mybatis介绍什么是 Mybatismybatis就是一个封装来jdbc的持久层框架，它和hibernate都属于ORM框架，但是具体的说，hibernate是一个完全的orm框架，而mybatis是一个不完全的orm框架。mybatis让程序员只关注sql本身，而不需要去关注如连接的创建，statement的创建等操作。mybatis会将输入的参数，输出结果进行映射。 Mybatis框架原理">
<meta name="twitter:image" content="http://om6odecqr.bkt.clouddn.com/mybatis%E5%8E%9F%E7%90%86%E5%9B%BE.png">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Mybatis快速入门一(基本应用)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Mybatis快速入门一(基本应用)</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-10T14:23:00.000Z" itemprop="datePublished" class="page-time">
  2017-08-10
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Mybatis介绍"><span class="post-toc-number">1.</span> <span class="post-toc-text">Mybatis介绍</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是-Mybatis"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">什么是 Mybatis</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Mybatis框架原理"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">Mybatis框架原理</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#入门程序"><span class="post-toc-number">2.</span> <span class="post-toc-text">入门程序</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#准备工作"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">准备工作</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#创建dao类"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">创建dao类</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#创建全局配置文件"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">创建全局配置文件</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#通过ID查询一行记录"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">通过ID查询一行记录</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-映射文件"><span class="post-toc-number">2.4.1.</span> <span class="post-toc-text">1 映射文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-测试代码"><span class="post-toc-number">2.4.2.</span> <span class="post-toc-text">2 测试代码</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-输出结果"><span class="post-toc-number">2.4.3.</span> <span class="post-toc-text">3 输出结果</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#模糊查询多行记录"><span class="post-toc-number">2.5.</span> <span class="post-toc-text">模糊查询多行记录</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-映射文件-1"><span class="post-toc-number">2.5.1.</span> <span class="post-toc-text">1 映射文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-测试代码-1"><span class="post-toc-number">2.5.2.</span> <span class="post-toc-text">2 测试代码</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-输出结果-1"><span class="post-toc-number">2.5.3.</span> <span class="post-toc-text">3 输出结果</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求：添加用户"><span class="post-toc-number">2.6.</span> <span class="post-toc-text">需求：添加用户</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-映射文件-2"><span class="post-toc-number">2.6.1.</span> <span class="post-toc-text">1 映射文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-测试代码-2"><span class="post-toc-number">2.6.2.</span> <span class="post-toc-text">2 测试代码</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-主键返回值之自增主键"><span class="post-toc-number">2.6.3.</span> <span class="post-toc-text">3 主键返回值之自增主键</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#4-主键返回值之UUID"><span class="post-toc-number">2.6.4.</span> <span class="post-toc-text">4 主键返回值之UUID</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#5-主键返回值之序列"><span class="post-toc-number">2.6.5.</span> <span class="post-toc-text">5 主键返回值之序列</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求：删除用户"><span class="post-toc-number">2.7.</span> <span class="post-toc-text">需求：删除用户</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-映射文件-3"><span class="post-toc-number">2.7.1.</span> <span class="post-toc-text">1 映射文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-测试代码-3"><span class="post-toc-number">2.7.2.</span> <span class="post-toc-text">2 测试代码</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#需求：修改用户"><span class="post-toc-number">2.8.</span> <span class="post-toc-text">需求：修改用户</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#1-映射文件-4"><span class="post-toc-number">2.8.1.</span> <span class="post-toc-text">1 映射文件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#2-测试代码-4"><span class="post-toc-number">2.8.2.</span> <span class="post-toc-text">2 测试代码</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#作用域（Scope）和生命周期"><span class="post-toc-number">3.</span> <span class="post-toc-text">作用域（Scope）和生命周期</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#SqlSessionFactoryBuilder"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">SqlSessionFactoryBuilder</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#SqlSessionFactory"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">SqlSessionFactory</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#SqlSession"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">SqlSession</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Mybatis_primary_01"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Mybatis快速入门一(基本应用)</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年08月10日 22:23" datetime="2017-08-10T14:23:00.000Z"  itemprop="datePublished">2017-08-10</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Mybatis介绍"><a href="#Mybatis介绍" class="headerlink" title="Mybatis介绍"></a>Mybatis介绍</h2><h3 id="什么是-Mybatis"><a href="#什么是-Mybatis" class="headerlink" title="什么是 Mybatis"></a>什么是 Mybatis</h3><p>mybatis就是一个封装来jdbc的持久层框架，它和hibernate都属于ORM框架，但是具体的说，hibernate是一个完全的orm框架，而mybatis是一个不完全的orm框架。mybatis让程序员只关注sql本身，而不需要去关注如连接的创建，statement的创建等操作。mybatis会将输入的参数，输出结果进行映射。</p>
<h3 id="Mybatis框架原理"><a href="#Mybatis框架原理" class="headerlink" title="Mybatis框架原理"></a>Mybatis框架原理</h3><p><img src="http://om6odecqr.bkt.clouddn.com/mybatis%E5%8E%9F%E7%90%86%E5%9B%BE.png" alt="mybatis原理图"></p>
<hr>
<h2 id="入门程序"><a href="#入门程序" class="headerlink" title="入门程序"></a>入门程序</h2><h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p><a href="https://github.com/mybatis/mybatis-3/releases" target="_blank" rel="external">mybatis下载地址</a></p>
<p><a href="">测试使用数据脚本</a></p>
<p>数据库截图：</p>
<p><img src="http://om6odecqr.bkt.clouddn.com/mybatis%E6%95%B0%E6%8D%AE%E5%BA%93%E6%88%AA%E5%9B%BE.png" alt="mybatis数据库截图"></p>
<p>初始化项目(Mybatis核心包和依赖包,Mysql驱动包)(注意test/config是Source folder文件夹):</p>
<p><img src="http://om6odecqr.bkt.clouddn.com/mybatis%E9%A1%B9%E7%9B%AE%E5%87%86%E5%A4%87.png" alt="mybatis工程初始化"></p>
<p>如果以上你无法完成就不用继续往下看了，看了你也看不懂。</p>
<h3 id="创建dao类"><a href="#创建dao类" class="headerlink" title="创建dao类"></a>创建dao类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//MybatisTest1/src/cn/sqdyy/mybatis/dao/User.java</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</div><div class="line">    <span class="keyword">private</span> String username;<span class="comment">// 用户姓名</span></div><div class="line">    <span class="keyword">private</span> String sex;<span class="comment">// 性别</span></div><div class="line">    <span class="keyword">private</span> Date birthday;<span class="comment">// 生日</span></div><div class="line">    <span class="keyword">private</span> String address;<span class="comment">// 地址</span></div></pre></td></tr></table></figure>
<h3 id="创建全局配置文件"><a href="#创建全局配置文件" class="headerlink" title="创建全局配置文件"></a>创建全局配置文件</h3><p>在config目录下，创建mybatis-config.xml文件，XML 配置文件的详细内容后面再探讨，这里先给出一个简单的示例：：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</div><div class="line"><span class="meta">&lt;!DOCTYPE configuration</span></div><div class="line"><span class="meta">  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"</span></div><div class="line"><span class="meta">  "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;</span></div><div class="line"><span class="comment">&lt;!-- 配置mybatis的环境文件 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></div><div class="line">      <span class="comment">&lt;!-- 配置JDBC事务控制，由mybatis进行管理 --&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></div><div class="line">      <span class="comment">&lt;!-- 配置数据源，采用mybatis的连接池 --&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"com.mysql.jdbc.Driver"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://localhost:3306/MybatisTest1?useUnicode=true&amp;amp;characterEncoding=utf8"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"root"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"1234"</span>/&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></div><div class="line">  </div><div class="line">  <span class="comment">&lt;!-- 加载映射文件 --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"User.xml"</span>/&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="通过ID查询一行记录"><a href="#通过ID查询一行记录" class="headerlink" title="通过ID查询一行记录"></a>通过ID查询一行记录</h3><h4 id="1-映射文件"><a href="#1-映射文件" class="headerlink" title="1 映射文件"></a>1 映射文件</h4><p>在config目录下，创建User.xml（这种命名规范是由ibatis遗留下来）<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</div><div class="line"><span class="meta">&lt;!DOCTYPE mapper</span></div><div class="line"><span class="meta">  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"</span></div><div class="line"><span class="meta">  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;</span></div><div class="line">  </div><div class="line"><span class="comment">&lt;!-- 注意：在mapper代理中，它具有特殊及重要的作用(后面会讲) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"test"</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- 根据用户ID查询用户信息 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- select：表示一个MappedStatement对象 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- id：代表statement的唯一标示 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- #&#123;&#125;：表示一个占位符 ? --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- #&#123;id&#125;：里面的id表示输入参数的参数名称，如果该参数是简单类型，那么#&#123;&#125;里面的参数名称可以任意 --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- parameterType：输入参数的java类型(可以写全限定类名) --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- resultTYpe：输出结果所映射的java类型(单条结果所对应的java类型) --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">    select * from user where id = #&#123;id&#125;;</div><div class="line">  <span class="tag">&lt;/<span class="name">select</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="2-测试代码"><a href="#2-测试代码" class="headerlink" title="2 测试代码"></a>2 测试代码</h4><p>在test目录下创建MybatisFirst.java测试类<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//cn.sqdyy.mybatis.test.MybatisFirst</span></div><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUserByIdTest</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">    <span class="comment">// 读取全局配置文件</span></div><div class="line">    String resource = <span class="string">"mybatis-config.xml"</span>;</div><div class="line">    InputStream inputStream = Resources.getResourceAsStream(resource);</div><div class="line">    </div><div class="line">    <span class="comment">// 创建SqlSessionFactory</span></div><div class="line">    SqlSessionFactory sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</div><div class="line">    </div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    </div><div class="line">    <span class="comment">// 调用SqlSession的增删改查方法</span></div><div class="line">    <span class="comment">// 第一个参数：表示statement的唯一标示，需要标明namespace</span></div><div class="line">    User user = sqlSession.selectOne(<span class="string">"test.findUserById"</span>, <span class="number">1</span>);</div><div class="line">    System.out.println(user);</div><div class="line">    </div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h4 id="3-输出结果"><a href="#3-输出结果" class="headerlink" title="3 输出结果"></a>3 输出结果</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">User [id=<span class="number">10</span>, username=张三, sex=<span class="number">1</span>, birthday=Thu Jul <span class="number">10</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span> CST <span class="number">2014</span>, address=北京市]</div></pre></td></tr></table></figure>
<h3 id="模糊查询多行记录"><a href="#模糊查询多行记录" class="headerlink" title="模糊查询多行记录"></a>模糊查询多行记录</h3><h4 id="1-映射文件-1"><a href="#1-映射文件-1" class="headerlink" title="1 映射文件"></a>1 映射文件</h4><p>在User.xml追加以下配置：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 根据用户名称模糊查询用户列表 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- $&#123;&#125;：表示一个sql的连接符 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- $&#123;value&#125;：里面的value表示输入参数的参数名称，如果该参数是简单类型，那么$&#123;&#125;里面的参数名称必须时value --&gt;</span></div><div class="line"><span class="comment">&lt;!-- $&#123;&#125;这种写法存在sql注入风险，所有要慎用，但某些场景下又不得不用： --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 比如排序时，动态传入排序的列名，这个时候需要手工地做好过滤工作，来防止sql注入攻击。 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 参考链接：http://www.cnblogs.com/sunada2005/p/4739312.html --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUsersByName"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.String"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  select * from user where username like '%$&#123;value&#125;%';</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h4 id="2-测试代码-1"><a href="#2-测试代码-1" class="headerlink" title="2 测试代码"></a>2 测试代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUsersByName</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">    String resource = <span class="string">"mybatis-config.xml"</span>;</div><div class="line">    InputStream inputStream = Resources.getResourceAsStream(resource);</div><div class="line">    SqlSessionFactory sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// ----------------------</span></div><div class="line">    List&lt;User&gt; list = sqlSession.selectList(<span class="string">"test.findUsersByName"</span>, <span class="string">"小明"</span>);</div><div class="line">    System.out.println(list);</div><div class="line">    <span class="comment">// ----------------------</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="3-输出结果-1"><a href="#3-输出结果-1" class="headerlink" title="3 输出结果"></a>3 输出结果</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[User [id=<span class="number">16</span>, username=张小明, sex=<span class="number">1</span>, birthday=<span class="keyword">null</span>, address=河南郑州], </div><div class="line">User [id=<span class="number">22</span>, username=陈小明, sex=<span class="number">1</span>, birthday=<span class="keyword">null</span>, address=河南郑州], </div><div class="line">User [id=<span class="number">25</span>, username=陈小明, sex=<span class="number">1</span>, birthday=<span class="keyword">null</span>, address=河南郑州]]</div></pre></td></tr></table></figure>
<h3 id="需求：添加用户"><a href="#需求：添加用户" class="headerlink" title="需求：添加用户"></a>需求：添加用户</h3><h4 id="1-映射文件-2"><a href="#1-映射文件-2" class="headerlink" title="1 映射文件"></a>1 映射文件</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 添加用户 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 通过OGNL去User对象中查找对应的属性名获得 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">	insert into user</div><div class="line">	(username,birthday,sex,address)</div><div class="line">	values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;);</div><div class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="2-测试代码-2"><a href="#2-测试代码-2" class="headerlink" title="2 测试代码"></a>2 测试代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insertUser</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">    String resource = <span class="string">"mybatis-config.xml"</span>;</div><div class="line">    InputStream inputStream = Resources.getResourceAsStream(resource);</div><div class="line">    SqlSessionFactory sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// ----------------------</span></div><div class="line">    User user = <span class="keyword">new</span> User();</div><div class="line">    user.setUsername(<span class="string">"神奇的鸭鸭"</span>);</div><div class="line">    user.setAddress(<span class="string">"南300计创"</span>);</div><div class="line">    sqlSession.insert(<span class="string">"test.insertUser"</span>, user);</div><div class="line">    <span class="comment">// 提交事务</span></div><div class="line">    sqlSession.commit();</div><div class="line">    <span class="comment">// ----------------------</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="3-主键返回值之自增主键"><a href="#3-主键返回值之自增主键" class="headerlink" title="3 主键返回值之自增主键"></a>3 主键返回值之自增主键</h4><p>获取主键返回值思路：</p>
<ul>
<li>mysql自增主键，时指在insert之前mysql会自动生成一个自增的主键</li>
<li>我们可以通过MySQL的函数获取到刚插入的自增主键：last_insert_id()</li>
<li>这个函数是在insert语句之后去调用的<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 添加用户 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 通过OGNL去User对象中查找对应的属性名获得 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- selectKey：查询主键，在标签内需要输入查询主键的sql --&gt;</span></div><div class="line"><span class="comment">&lt;!-- order：指定查询主键的sql和insert语句的执行顺序，相当于insert语句来说（当前在insert语句执行后才生成主键id，所有应该填after） --&gt;</span></div><div class="line"><span class="comment">&lt;!-- last_insert_id：该函数是mysql的函数，获取自增主键的id，它必须配合insert语句一起使用 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">selectKey</span> <span class="attr">keyProperty</span>=<span class="string">"id"</span> <span class="attr">resultType</span>=<span class="string">"int"</span> <span class="attr">order</span>=<span class="string">"AFTER"</span>&gt;</span></div><div class="line">	  select last_insert_id();</div><div class="line">	<span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span></div><div class="line">	insert into user</div><div class="line">	(username,birthday,sex,address)</div><div class="line">	values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;);</div><div class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="4-主键返回值之UUID"><a href="#4-主键返回值之UUID" class="headerlink" title="4 主键返回值之UUID"></a>4 主键返回值之UUID</h4><p>注意：</p>
<ul>
<li>使用mysql的uuid()函数生成主键，需要修改表中的id字段类型为string，长度设置为35位。</li>
<li>此时order采用before，因为需要先生成出主键，再执行insert语句</li>
<li>需要显式给id赋值<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">selectKey</span> <span class="attr">keyProperty</span>=<span class="string">"id"</span> <span class="attr">resultType</span>=<span class="string">"string"</span> <span class="attr">order</span>=<span class="string">"BEFORE"</span>&gt;</span></div><div class="line">	  select uuid();</div><div class="line">	<span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span></div><div class="line">	insert into user</div><div class="line">	(id,username,birthday,sex,address)</div><div class="line">	values(#&#123;id&#125;,#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;);</div><div class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="5-主键返回值之序列"><a href="#5-主键返回值之序列" class="headerlink" title="5 主键返回值之序列"></a>5 主键返回值之序列</h4><ul>
<li>通过Oracle的sequence获取主键方式(与MySql的uuid方式基本一致)<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">selectKey</span> <span class="attr">keyProperty</span>=<span class="string">"id"</span> <span class="attr">resultType</span>=<span class="string">"int"</span> <span class="attr">order</span>=<span class="string">"BEFORE"</span>&gt;</span></div><div class="line">	  select user_seq.nextval() from dual;</div><div class="line">	<span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span></div><div class="line">	insert into user</div><div class="line">	(id,username,birthday,sex,address)</div><div class="line">	values(#&#123;id&#125;,#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;);</div><div class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="需求：删除用户"><a href="#需求：删除用户" class="headerlink" title="需求：删除用户"></a>需求：删除用户</h3><h4 id="1-映射文件-3"><a href="#1-映射文件-3" class="headerlink" title="1 映射文件"></a>1 映射文件</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 根据id删除用户 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"deleteUser"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>&gt;</span></div><div class="line">  delete from user where id = #&#123;id&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">delete</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="2-测试代码-3"><a href="#2-测试代码-3" class="headerlink" title="2 测试代码"></a>2 测试代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sqlSession.delete(<span class="string">"test.deleteUser"</span>, <span class="number">28</span>);</div><div class="line"><span class="comment">// 提交事务</span></div><div class="line">sqlSession.commit();</div></pre></td></tr></table></figure>
<h3 id="需求：修改用户"><a href="#需求：修改用户" class="headerlink" title="需求：修改用户"></a>需求：修改用户</h3><h4 id="1-映射文件-4"><a href="#1-映射文件-4" class="headerlink" title="1 映射文件"></a>1 映射文件</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 根据传入的用户信息修改用户 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  update user set username=#&#123;username&#125;, sex=#&#123;sex&#125; where id=#&#123;id&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">update</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="2-测试代码-4"><a href="#2-测试代码-4" class="headerlink" title="2 测试代码"></a>2 测试代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">User user = <span class="keyword">new</span> User();</div><div class="line">user.setId(<span class="number">27</span>);</div><div class="line">user.setUsername(<span class="string">"神奇的鸭鸭2"</span>);</div><div class="line">user.setAddress(<span class="string">"南300计创2"</span>);</div><div class="line">sqlSession.insert(<span class="string">"test.updateUser"</span>, user);</div><div class="line"><span class="comment">// 提交事务</span></div><div class="line">sqlSession.commit();</div></pre></td></tr></table></figure>
<hr>
<h2 id="作用域（Scope）和生命周期"><a href="#作用域（Scope）和生命周期" class="headerlink" title="作用域（Scope）和生命周期"></a>作用域（Scope）和生命周期</h2><p>完成上面的基本增删改查操作，就算打开了mybatis的一扇大门，但是在测试代码中，有大量重复代码，我们第一反应是给它抽取出共性的部分，但是SqlSession、SqlSessionFactory、SqlSessionFactoryBuilder有着各自的生命周期，因为这些生命周期的不同，抽取时要有针对性的处理，本期将不会涉及到抽取内容，而是讲述他们三个的生命周期。</p>
<h3 id="SqlSessionFactoryBuilder"><a href="#SqlSessionFactoryBuilder" class="headerlink" title="SqlSessionFactoryBuilder"></a>SqlSessionFactoryBuilder</h3><p>它的作用只是通过配置文件创建SqlSessionFactory，所以只要创建出SqlSessionFactory，它就可以销毁了。<strong>所以他的生命周期应该是在方法之内。</strong>你可以重用 SqlSessionFactoryBuilder 来创建多个 SqlSessionFactory 实例，但是最好还是不要让其一直存在以保证所有的 XML 解析资源开放给更重要的事情。</p>
<h3 id="SqlSessionFactory"><a href="#SqlSessionFactory" class="headerlink" title="SqlSessionFactory"></a>SqlSessionFactory</h3><p>它的作用时创建SqlSession的工厂，工厂一旦创建，除非应用停掉，不要销毁。<strong>所以他的生命周期应该是在应用范围之内。</strong>多次重建SqlSessionFactory被视为一种代码“坏味道（bad smell）”。这里可以通过单例模式来管理它。在mybatis和spring整合之后，最好的处理方式是把SqkSessionFactory交由spring来做单例管理。</p>
<h3 id="SqlSession"><a href="#SqlSession" class="headerlink" title="SqlSession"></a>SqlSession</h3><p>SqlSession是一个面向用户(程序员)的借口，它的默认实现时DefaultSqlSession，Mybatis时通过SqlSession来操作数据库的。SqlSession中不仅包含要处理的SQL信息，还包括一些数据信息，所以说他是线程不安全的。<strong>所以他的生命周期应该是在方法体之内。</strong>绝对不能将 SqlSession 实例的引用放在一个类的静态域，甚至一个类的实例变量也不行。也绝不能将 SqlSession 实例的引用放在任何类型的管理作用域中，比如 Servlet 架构中的 HttpSession。在Web框架中，每次收到的HTTP请求，就可以打开一个SqlSession，放回一个响应，就应该关闭它，这个关闭操作是很重要的，应该把它放到finally块中确保每次都能执行关闭：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">SqlSession session = sqlSessionFactory.openSession();</div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">  <span class="comment">// do work</span></div><div class="line">&#125; <span class="keyword">finally</span> &#123;</div><div class="line">  session.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:57:52.006Z" itemprop="dateUpdated">2017年12月5日 17:57</time>
</span><br>


        如要转载请注明出处：<a href="/2017/08/10/Mybatis_primary_01/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SSM/">SSM</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&title=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&title=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/13/Mybatis_primary_02/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Mybatis快速入门二(DAO开发思路)</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/08/Maven_primary_01/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Maven快速入门</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&title=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&title=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门一(基本应用)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/08/10/Mybatis_primary_01/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
